package com.example.blog_system.web.client;

import com.example.blog_system.model.domain.Article;
import com.example.blog_system.service.IArticleService;
import com.example.blog_system.utils.Commons;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
//    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private IArticleService articleServiceImpl;
    @GetMapping(value = "/")
    private String index(HttpServletRequest request){
        return this.index(request,1,5);
    }

    @GetMapping(value = "/page/{p}")
    public String index(HttpServletRequest request, @PathVariable("p") int page, @RequestParam(value = "count",defaultValue = "5") int count){
        PageInfo<Article> articles = articleServiceImpl.selectArticleWithPage(page,count);
        List<Article> articleList = articleServiceImpl.getHeatArticles();
        Commons commons=new Commons();
        request.setAttribute("articles",articles);
        request.setAttribute("articleList",articleList);
        request.setAttribute("commons",commons);
//        logger.info("分页获取文章信息：页码"+page+"，条数"+count);
        return "client/index.html";
    }

    @GetMapping(value = "/article/{id}")
    public String getArticleById(@PathVariable("id") Integer id, HttpServletRequest request){
        Article article = articleServiceImpl.selectArticleWithId(id);
        if (article!=null){
            request.setAttribute("article",article);
            return "client/articleDetails";
        }else {
            return "comm/error_404";
        }
    }
}
